package com.yunfei.commonbase.config;

import com.yunfei.commonbase.exception.DataException;
import com.yunfei.commonbase.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;




//TODO  这个可能有问题






/**
 * @ClassName GlobalResponseExceptionHandler
 * @Package com.pktech.osmc.bo.common.config
 * @Description 全局异常管理器
 * @author hugp
 * @date 2019年6月19日 上午12:28:27
 */
@ControllerAdvice
public class BoGlobalResponseExceptionHandler {

    /**
     * @Title exceptionHandler
     * @Description 处理框架自动抛出的所有异常
     * @param httpServletRequest
     * @param e
     * @return ResponseErrorJsonBean
     * @throws
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {

        //显示详细异常信息，便于开发人员定位问题
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw, true));
        //String errorMessage = "Class:" + e.getClass() + ";Cause:" + e.getCause() + ";" + "Message:" + e.getMessage();
        String errorMessage = sw.toString();
        return new Result(new Result("1",  errorMessage));
    }

    /**
     * @Title businessExceptionHandler
     * @Description 处理自定义抛出的异常
     * @param httpServletRequest
     * @param e
     * @return ResponseErrorJsonBean
     * @throws
     */
    @ResponseBody
    @ExceptionHandler(value = DataException.class)
    public Result businessExceptionHandler(HttpServletRequest httpServletRequest, DataException e) {
        return new Result(e.getStatus(), e.getMsg());
    }
}

